package rest.rabarbar.threads;

import javax.servlet.ServletContextEvent;

import java.io.DataOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;

import javax.servlet.ServletContextListener;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.api.json.JSONConfiguration;

import rest.rabarbar.convert.ConvertJson;
import rest.rabarbar.services.GetFromServer;

public class Notificator extends TimerTask implements ServletContextListener {
	private Thread thread;
	private List idList;
	
	
	@Override
	public void contextDestroyed(ServletContextEvent arg0) {
		this.thread.interrupt();

	}

	@Override
	public void contextInitialized(ServletContextEvent arg0) {
		thread = new Thread(this);
		thread.start();


	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		while (true) {

			/*		try {
		String st = new String ("WWWW ");

				ClientConfig clientConfig = new DefaultClientConfig();

				clientConfig.getFeatures().put(
						JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);

				Client client = Client.create(clientConfig);

				WebResource webResource = client
						.resource("http://localhost:9090/RestClient/send");

				ClientResponse response = webResource.accept("application/json")
						.type("application/json").post(ClientResponse.class, st);

				if (response.getStatus() != 200) {
					throw new RuntimeException("Failed : HTTP error code : "
							+ response.getStatus());
				}

				String output = response.getEntity(String.class);

				System.out.println("Server response us! \n");
				System.out.println(output);
				
				

			} catch (Exception e) {

				e.printStackTrace();

			}*/
/*
			try {
				String url = "http://localhost:9090/RestClient/updatelist";

			    URL obj = new URL(url);
			    HttpURLConnection con = (HttpURLConnection) obj.openConnection();
String responseFromServer = GetFromServer.getFromServer("http://localhost:8080/BookstoreTest/bookstore/getAllId");
			    // optional default is GET
			    con.setRequestMethod("GET");
			            con.setDoInput(true);
			            con.setDoOutput(true);
			            DataOutputStream dos = new DataOutputStream(con.getOutputStream());
			            dos.writeBytes(responseFromServer);
			            System.out.println("AAAAAAAaa" + responseFromServer);
				TimeUnit.MINUTES.sleep(10);
			} catch (InterruptedException | IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			/*
			 * pobieranie danych co 10 minut
			 */
			
		}
		
	}

}
